package com.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Processor {

	public static void main(String[] args) {
		
	}

	//获取网页脚本的所有内容，如果有特殊处理，则使用overload特性
	public abstract String getContentByCon(String entranceUrl,String cutStartString, String cutEndString);
	
	public static String getContent(String entranceUrl){
		StringBuffer sb = new StringBuffer();
		DataInputStream dis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			URL url = new URL(entranceUrl);
			//得到charset
			String charset = SinoDetect.getInstance().getCharset(url);
			HttpURLConnection uc = (HttpURLConnection) url.openConnection();
			uc.connect();
			int code = uc.getResponseCode();
			if (code != 200)
				return sb.toString();
			dis = new DataInputStream(uc.getInputStream());
			isr = new InputStreamReader(dis, charset);
			br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				sb.append(line.trim());
				line = null;
			}
		} catch (MalformedURLException e) {
			System.out.println("getContent异常 1 : " + e.toString());
		} catch (IOException e) {
			System.out.println("getContent异常 2 : " + e.toString());
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (isr != null)
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (dis != null)
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return sb.toString();
	}
	
	public abstract String getValueByCon(String content, String regex, String index);
	
	public static String getValue(String content, String regex) {
		String result = "";
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		if (m.find()) {
			result = m.group(m.groupCount());
			result = processResult(result);
		}
		return result.trim();
	}
	
	public static String[] getAllValue(String content, String regex) {
		String result[] = null;
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		String tempString = "";
		while (m.find()) {
			tempString = m.group(m.groupCount());
			tempString = processResult(tempString);
			list.add(tempString);
		}
		result = StringTools.convertListToStrArr(list);
		return result;
	}

	
	// 返回含tag的一段内容
	public static String processResult(String sen) {
		sen = sen.replaceAll("&lt;", "<");
		sen = sen.replaceAll("&gt;", ">");
		sen = sen.replaceAll("&nbsp;", " ");//
		sen = sen.replaceAll("&ldquo;", "\"");
		sen = sen.replaceAll("&rdquo;", "\"");
		sen = sen.replaceAll("&quot;", "\"");
		sen = sen.replaceAll("&mdash;", "-");
		sen = sen.replaceAll("&hellip;", "...");
		sen = sen.replaceAll("&#183;", "·");
		sen = sen.replaceAll("&#0?39;", "'");
		sen = sen.replaceAll("&#8207;", "");
		sen = sen.replaceAll("&hellip;", "...");
		sen = sen.replaceAll("‘", "'");// ‘发烧’
		sen = sen.replaceAll("’", "'");
		sen = sen.replaceAll("&amp;", "&");
		sen = sen.replaceAll("'+", "'");
		sen = sen.replaceAll("  +", " ").trim();
		sen = sen.replaceAll("&.{2,8};", "");
		sen = sen.replaceAll("<br>", " ");
		sen = sen.replaceAll("\\t", " ");
		sen = sen.replaceAll("'", "''");
		return sen.trim();
	}
}
